package cn.rxls.community.service.impl;


import cn.rxls.community.dto.complaintFeedback.AddComplaintFeedbackDTO;
import cn.rxls.community.entity.ComplaintFeedback;
import cn.rxls.community.enums.ComplaintFeedbackTypeEnum;
import cn.rxls.community.enums.ResultEnum;
import cn.rxls.community.exception.BusinessException;
import cn.rxls.community.mapper.ComplaintFeedbackMapper;
import cn.rxls.community.service.ComplaintFeedbackService;
import cn.rxls.community.util.HttpUtil;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Objects;
import java.util.Optional;

/**
 * 投诉反馈(ComplaintFeedback)表服务实现类
 */
@Service("complaintFeedbackService")
@Transactional
public class ComplaintFeedbackServiceImpl extends ServiceImpl<ComplaintFeedbackMapper, ComplaintFeedback> implements ComplaintFeedbackService {


    private final ComplaintFeedbackMapper complaintFeedbackMapper;

    ComplaintFeedbackServiceImpl(ComplaintFeedbackMapper complaintFeedbackMapper) {
        this.complaintFeedbackMapper = complaintFeedbackMapper;
    }

    /**
     * 新增投诉反馈
     *
     * @param dto 参数对象
     * @return 是否成功
     */
    @Override
    public Boolean addComplaintFeedback(AddComplaintFeedbackDTO dto) {
        String uid = String.valueOf(HttpUtil.getUserLoginInfo().get("uid"));
        ComplaintFeedback complaintFeedback = new ComplaintFeedback();
        complaintFeedback.setComplaintFeedbackMsg(dto.getMsg());
        complaintFeedback.setComplaintFeedbackImg(dto.getImg());
        complaintFeedback.setComplaintFeedbackUser(Long.valueOf(uid));
        Integer codeByMsg = ComplaintFeedbackTypeEnum.getCodeByMsg(dto.getCategory());
        Optional.ofNullable(codeByMsg).orElseThrow(()->new BusinessException(ResultEnum.ERROR_COMPLAINT_FEEDBACK_TYPE));
        complaintFeedback.setComplaintFeedbackCategory(codeByMsg);

        return complaintFeedbackMapper.insert(complaintFeedback) > 0;
    }
}

